<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Api\PermisionRequest;
use App\Transformers\PermissionTransformer;
use Spatie\Permission\Models\Permission;

class PermissionController extends BaseController
{
    /**
     * 权限列表
     */
    public function index()
    {
        $permissions = Permission::paginate(20);  // 分页
//        响应生成器
        return $this->response->paginator($permissions,new PermissionTransformer());
    }
    /**
     * 添加权限
     */
    public function store(PermisionRequest $request)
    {
        $request->offsetSet('guard_name', 'api');
//        批量赋值
        Permission::create($request->all());

        return $this->response->created();
    }

    /**
     * 更新权限
     */
    public function update(PermisionRequest $request, Permission $permission)
    {
        $request->offsetSet('guard_name', 'api');
//        批量赋值
        $permission->update($request->all());
        return $this->response->noContent();
    }

    /**
     * 删除权限
     */
    public function destroy(Permission $permission)
    {
        $permission->delete();
        return $this->response->noContent();
    }
}
